package com.ybkj.daijia.api.common.v4up;

import com.ybkj.daijia.api.util.ErrorConstantV4;
import com.ybkj.daijia.api.util.ResultTransport;
import com.ybkj.daijia.server.mc.Passenger;
import com.ybkj.daijia.server.mc.PassengerGrade;
import com.ybkj.daijia.server.mc.PassengerGradeAndArea;
import com.ybkj.daijia.server.model.Area;
import com.ybkj.daijia.server.service.AreaService;
import com.ybkj.daijia.server.service.MemberService;
import com.ybkj.daijia.server.service.ZhuancheService;
import com.ybkj.daijia.server.zhuan.Order.ServiceType;
import java.util.List;
import java.util.Map;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller("CommonControllerV4up")
@RequestMapping(value = "common/api/rest/v4up")
@Slf4j
public class CommonController {


    @Autowired
    private ZhuancheService zhuancheService;
    @Autowired
    private MemberService memberService;
    @Autowired
    private AreaService areaService;

    /**
     * 专车订单费用计算
     *
     * @param areaId         收费标准id
     * @param time           分钟 载客时间
     * @param mileage        公里 载客里程
     * @param passengerPhone 客户电话
     * @param serviceType    服务类型
     * @param carTypeId      车辆类型id
     * @return
     */
    @RequestMapping(value = "getZhuanPrice", method = RequestMethod.GET)
    public synchronized @ResponseBody
    ResultTransport getZhuanPrice(Long areaId, Integer time, Double mileage, String passengerPhone,
        String serviceType, @RequestParam(required = false, value = "carTypeId") Long carTypeId) {

        log.info(
            "recv areaId:{},time:{},mileage:{},passengerPhone:{},serviceType:{},appKey:{},token:{},timestamp:{}",
            areaId, time, mileage,
            passengerPhone, serviceType);

        // 参数验证
        if (null == areaId || null == time || null == mileage || StringUtils.isBlank(passengerPhone)
            || StringUtils.isBlank(serviceType)) {
            return ResultTransport.getErrorBy(ErrorConstantV4.PARAM_ERROR);
        }

        try {
            Long newAreaId = null;
            Area area = null;

            // 根据客户电话查出对应客户信息
            Passenger passenger = memberService.findOnePassengerByPhone(passengerPhone);
            if (null != passenger) {
                if (null != passenger.getGrade_id()) {
                    // 查询客户等级信息
                    PassengerGrade passengerGrade = memberService
                        .findOneGrade(passenger.getGrade_id());
                    if (null != passengerGrade) {
                        List<PassengerGradeAndArea> passengerGradeAndAreas = passengerGrade
                            .getPassengerGradeAndAreas();
                        PassengerGradeAndArea passengerGradeAndArea = null;

                        if (null != carTypeId && carTypeId != 0 && !CollectionUtils
                            .isEmpty(passengerGradeAndAreas)) {
                            for (PassengerGradeAndArea pgaa : passengerGradeAndAreas) {
                                if (pgaa.getCarTypeId().equals(carTypeId)) {
                                    passengerGradeAndArea = pgaa;
                                }
                            }
                        }

                        if (ServiceType.valueOf(serviceType).compareTo(ServiceType.tangzu) == 0) {
                            if (null != passengerGradeAndArea && null != passengerGradeAndArea
                                .getTzAreaId()) {
                                Area one = areaService
                                    .findOneWithCharge(passengerGradeAndArea.getTzAreaId());
                                if (one != null) {
                                    area = one;
                                }
                            } else if (null != passengerGrade.getTzAreaId()) {
                                Area one = areaService.findOne(passengerGrade.getTzAreaId());
                                if (one != null) {
                                    area = one;
                                }
                            }
                        } else if (ServiceType.valueOf(serviceType).compareTo(ServiceType.banrizu)
                            == 0) {
                            if (null != passengerGradeAndArea && null != passengerGradeAndArea
                                .getTzAreaId()) {
                                Area one = areaService
                                    .findOneWithCharge(passengerGradeAndArea.getBrzAreaId());
                                if (one != null) {
                                    area = one;
                                }
                            } else if (null != passengerGrade.getBrzAreaId()) {
                                Area one = areaService.findOne(passengerGrade.getBrzAreaId());
                                if (one != null) {
                                    area = one;
                                }
                            }
                        } else if (ServiceType.valueOf(serviceType).compareTo(ServiceType.rizu)
                            == 0) {
                            if (null != passengerGradeAndArea && null != passengerGradeAndArea
                                .getRzAreaId()) {
                                Area one = areaService
                                    .findOneWithCharge(passengerGradeAndArea.getRzAreaId());
                                if (one != null) {
                                    area = one;
                                }
                            } else if (null != passengerGrade.getRzAreaId()) {
                                Area one = areaService.findOne(passengerGrade.getRzAreaId());
                                if (one != null) {
                                    area = one;
                                }
                            }
                        } else if (ServiceType.valueOf(serviceType).compareTo(ServiceType.jieji)
                            == 0) {
                            if (null != passengerGradeAndArea && null != passengerGradeAndArea
                                .getJjAreaId()) {
                                Area one = areaService
                                    .findOneWithCharge(passengerGradeAndArea.getJjAreaId());
                                if (one != null) {
                                    area = one;
                                }
                            } else if (null != passengerGrade.getJjAreaId()) {
                                Area one = areaService.findOne(passengerGrade.getJjAreaId());
                                if (one != null) {
                                    area = one;
                                }
                            }
                        } else if (ServiceType.valueOf(serviceType).compareTo(ServiceType.songji)
                            == 0) {
                            if (null != passengerGradeAndArea && null != passengerGradeAndArea
                                .getSjAreaId()) {
                                Area one = areaService
                                    .findOneWithCharge(passengerGradeAndArea.getSjAreaId());
                                if (one != null) {
                                    area = one;
                                }
                            } else if (null != passengerGrade.getSjAreaId()) {
                                Area one = areaService.findOne(passengerGrade.getSjAreaId());
                                if (one != null) {
                                    area = one;
                                }
                            }
                        } else if (ServiceType.valueOf(serviceType).compareTo(ServiceType.jiezhan)
                            == 0) {
                            if (null != passengerGradeAndArea && null != passengerGradeAndArea
                                .getJzAreaId()) {
                                Area one = areaService
                                    .findOneWithCharge(passengerGradeAndArea.getJzAreaId());
                                if (one != null) {
                                    area = one;
                                }
                            } else if (null != passengerGrade.getJzAreaId()) {
                                Area one = areaService.findOne(passengerGrade.getJzAreaId());
                                if (one != null) {
                                    area = one;
                                }
                            }
                        } else if (ServiceType.valueOf(serviceType).compareTo(ServiceType.songzhan)
                            == 0) {
                            if (null != passengerGradeAndArea && null != passengerGradeAndArea
                                .getSzAreaId()) {
                                Area one = areaService
                                    .findOneWithCharge(passengerGradeAndArea.getSzAreaId());
                                if (one != null) {
                                    area = one;
                                }
                            } else if (null != passengerGrade.getSzAreaId()) {
                                Area one = areaService.findOne(passengerGrade.getSzAreaId());
                                if (one != null) {
                                    area = one;
                                }
                            }
                        }
                    }
                }
            }
            if (null != area) {
                newAreaId = area.getId();
            }

            if (null == newAreaId) {
                newAreaId = areaId;
            }

            Map<String, Object> map = zhuancheService
                .getNewPrice(newAreaId, mileage, Long.valueOf(time));
            map.put("time", time);
            map.put("mileage", mileage);
            map.put("newAreaId", newAreaId);
            ResultTransport instance = ResultTransport.getErrorBy(ErrorConstantV4.SUCCESS);
            instance.setData(map);
            return instance;
        } catch (Exception e) {
            log.error("api v4up getZhuanPrice error: {}", e);
            return ResultTransport.getErrorBy(ErrorConstantV4.SERVER_ERROR);
        }

    }

}
